


// Author:					HungNM
// Created:					16-08-2012
// Last Modified:			16-08-2012
//
	
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using eProject.Data;
	
namespace eProject.Business
{
	
	public class OrderDetails
	{

		#region Constructors

	    public OrderDetails()
		{}
	    
	
	    public OrderDetails(
			long id) 
		{
	        GetOrderDetails(
				id); 
	    }

		#endregion

		#region Private Properties

		private long id;
		private long userId;
		private long scheduleId;
		private int seatClassId = -1;
		private decimal ticketPrice;
		private int totalTicket = -1;
		private decimal totalPrice;
		private DateTime orderDate = DateTime.UtcNow;
		private short status;
		private DateTime createdDate = DateTime.UtcNow;
		private DateTime lastUpdate = DateTime.UtcNow;
		
		#endregion

		#region Public Properties

		public long Id 
		{
			get { return id; }
			set { id = value; }
		}
		public long UserId 
		{
			get { return userId; }
			set { userId = value; }
		}
		public long ScheduleId 
		{
			get { return scheduleId; }
			set { scheduleId = value; }
		}
		public int SeatClassId 
		{
			get { return seatClassId; }
			set { seatClassId = value; }
		}
		public decimal TicketPrice 
		{
			get { return ticketPrice; }
			set { ticketPrice = value; }
		}
		public int TotalTicket 
		{
			get { return totalTicket; }
			set { totalTicket = value; }
		}
		public decimal TotalPrice 
		{
			get { return totalPrice; }
			set { totalPrice = value; }
		}
		public DateTime OrderDate 
		{
			get { return orderDate; }
			set { orderDate = value; }
		}
		public short Status 
		{
			get { return status; }
			set { status = value; }
		}
		public DateTime CreatedDate 
		{
			get { return createdDate; }
			set { createdDate = value; }
		}
		public DateTime LastUpdate 
		{
			get { return lastUpdate; }
			set { lastUpdate = value; }
		}

		#endregion

		#region Private Methods

		/// <summary>
		/// Gets an instance of OrderDetails.
		/// </summary>
		/// <param name="id"> id </param>
		private void GetOrderDetails(
			long id) 
		{
			try
			{
				using(IDataReader reader = DBOrderDetails.GetOne(
					id)) 
				{
					PopulateFromReader(reader);
				}
			}
			catch(Exception ex)
			{
				throw ex;
			}
		}
		
		
		private void PopulateFromReader(IDataReader reader) 
		{
			try
			{
				if(reader.Read())
				{
					this.id = Convert.ToInt64(reader["Id"]);
					this.userId = Convert.ToInt64(reader["UserId"]);
					this.scheduleId = Convert.ToInt64(reader["ScheduleId"]);
					this.seatClassId = Convert.ToInt32(reader["SeatClassId"]);
					this.ticketPrice = Convert.ToDecimal(reader["TicketPrice"]);
					this.totalTicket = Convert.ToInt32(reader["TotalTicket"]);
					this.totalPrice = Convert.ToDecimal(reader["TotalPrice"]);
					this.orderDate = Convert.ToDateTime(reader["OrderDate"]);
					this.status = Convert.ToInt16(reader["Status"]);
					this.createdDate = Convert.ToDateTime(reader["CreatedDate"]);
					this.lastUpdate = Convert.ToDateTime(reader["LastUpdate"]);
				
				}
			}
			catch(Exception ex)
			{
				throw ex;
			}
		}
		
		/// <summary>
        /// Persists a new instance of OrderDetails. Returns true on success.
        /// </summary>
        /// <returns></returns>
		private bool Create()
		{ 
			int newID = 0;
			
			try
			{
				newID = DBOrderDetails.Create(
					this.userId, 
					this.scheduleId, 
					this.seatClassId, 
					this.ticketPrice, 
					this.totalTicket, 
					this.totalPrice, 
					this.orderDate, 
					this.status, 
					this.createdDate, 
					this.lastUpdate); 
					
				this.id = newID;
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return (newID > 0);
		}

		
		/// <summary>
        /// Updates this instance of OrderDetails. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
		private bool Update()
		{
			bool IsUpdated = false;
			try
			{
				IsUpdated = DBOrderDetails.Update(
					this.id, 
					this.userId, 
					this.scheduleId, 
					this.seatClassId, 
					this.ticketPrice, 
					this.totalTicket, 
					this.totalPrice, 
					this.orderDate, 
					this.status, 
					this.createdDate, 
					this.lastUpdate); 
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return IsUpdated;
		}

		#endregion

		#region Public Methods

		/// <summary>
        /// Saves this instance of OrderDetails. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
		public bool Save()
		{
			bool IsSaved = false;
			try
			{
				if(this.id > 0)
				{
					IsSaved = Update();
				}
				else
				{
					IsSaved = Create();
				}
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return IsSaved;
		}
		
		#endregion

		#region Static Methods

		/// <summary>
		/// Deletes an instance of OrderDetails. Returns true on success.
		/// </summary>
		/// <param name="id"> id </param>
		/// <returns>bool</returns>
		public static bool Delete(
			long  id) 
		{
			bool IsDeleted = false;
			try
			{
				IsDeleted = DBOrderDetails.Delete(
					id); 
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return IsDeleted;
		}
		
		
		/// <summary>
		/// Gets a count of OrderDetails. 
		/// </summary>
		public static int GetCount()
		{
			int count = 0;
			try
			{
				count = DBOrderDetails.GetCount();
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return count;
		}
	
		private static List<OrderDetails> LoadListFromReader(IDataReader reader)
		{
			List<OrderDetails> orderDetailsList = new List<OrderDetails>();
			try
			{
				while (reader.Read())
				{
					OrderDetails orderDetails = new OrderDetails();
					orderDetails.id = Convert.ToInt64(reader["Id"]);
					orderDetails.userId = Convert.ToInt64(reader["UserId"]);
					orderDetails.scheduleId = Convert.ToInt64(reader["ScheduleId"]);
					orderDetails.seatClassId = Convert.ToInt32(reader["SeatClassId"]);
					orderDetails.ticketPrice = Convert.ToDecimal(reader["TicketPrice"]);
					orderDetails.totalTicket = Convert.ToInt32(reader["TotalTicket"]);
					orderDetails.totalPrice = Convert.ToDecimal(reader["TotalPrice"]);
					orderDetails.orderDate = Convert.ToDateTime(reader["OrderDate"]);
					orderDetails.status = Convert.ToInt16(reader["Status"]);
					orderDetails.createdDate = Convert.ToDateTime(reader["CreatedDate"]);
					orderDetails.lastUpdate = Convert.ToDateTime(reader["LastUpdate"]);
					orderDetailsList.Add(orderDetails);
					
				}
			}
			finally
			{
				reader.Close();
			}
	
			return orderDetailsList;
		}
	
		/// <summary>
		/// Gets an IList with all instances of OrderDetails.
		/// </summary>
		public static List<OrderDetails> GetAll()
		{
			List<OrderDetails> listOrderDetails = new List<OrderDetails>();
			try
			{
				IDataReader reader = DBOrderDetails.GetAll();
				listOrderDetails = LoadListFromReader(reader);
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return listOrderDetails;
		}
	
		/// <summary>
		/// Gets an IList with page of instances of OrderDetails.
		/// </summary>
		/// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalPages">total pages</param>
		public static List<OrderDetails> GetPage(int pageNumber, int pageSize, out int totalPages)
		{
			totalPages = 1;
			List<OrderDetails> listOrderDetails = new List<OrderDetails>();
			try
			{
				IDataReader reader = DBOrderDetails.GetPage(pageNumber, pageSize, out totalPages);	
				listOrderDetails = LoadListFromReader(reader);
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return listOrderDetails;
		}
		
		#endregion

	}
}





